{% extends 'base.html' %}
{% load static %}

{% block title %}借用申请详情 - {{ borrow.asset.name }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <!-- 申请基本信息 -->
            <div class="card mb-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="fas fa-handshake me-2"></i>借用申请详情</h5>
                    <div>
                        {% if can_approve %}
                            <a href="{% url 'assets:borrow_request_approve' borrow.pk %}" class="btn btn-success btn-sm">
                                <i class="fas fa-check me-1"></i>审批
                            </a>
                        {% elif can_borrow_out %}
                            <a href="{% url 'assets:borrow_asset_out' borrow.pk %}" class="btn btn-info btn-sm">
                                <i class="fas fa-arrow-right me-1"></i>办理借出
                            </a>
                        {% elif can_return %}
                            <a href="{% url 'assets:return_asset' borrow.pk %}" class="btn btn-warning btn-sm">
                                <i class="fas fa-arrow-left me-1"></i>办理归还
                            </a>
                        {% endif %}
                        <a href="{% url 'assets:borrow_request_list' %}" class="btn btn-secondary btn-sm">
                            <i class="fas fa-arrow-left me-1"></i>返回列表
                        </a>
                    </div>
                </div>
                
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <h6 class="mb-3"><i class="fas fa-laptop me-2"></i>资产信息</h6>
                            <table class="table table-borderless">
                                <tr>
                                    <td width="120"><strong>资产名称：</strong></td>
                                    <td>{{ borrow.asset.name }}</td>
                                </tr>
                                <tr>
                                    <td><strong>资产编号：</strong></td>
                                    <td><code>{{ borrow.asset.asset_number }}</code></td>
                                </tr>
                                <tr>
                                    <td><strong>资产类别：</strong></td>
                                    <td>{{ borrow.asset.category.name }}</td>
                                </tr>
                                <tr>
                                    <td><strong>当前状态：</strong></td>
                                    <td>
                                        {% if borrow.asset.status == 'idle' %}
                                            <span class="badge bg-secondary">{{ borrow.asset.get_status_display }}</span>
                                        {% elif borrow.asset.status == 'in_use' %}
                                            <span class="badge bg-primary">{{ borrow.asset.get_status_display }}</span>
                                        {% elif borrow.asset.status == 'borrowed' %}
                                            <span class="badge bg-info">{{ borrow.asset.get_status_display }}</span>
                                        {% elif borrow.asset.status == 'maintenance' %}
                                            <span class="badge bg-warning">{{ borrow.asset.get_status_display }}</span>
                                        {% else %}
                                            <span class="badge bg-light text-dark">{{ borrow.asset.get_status_display }}</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>所属部门：</strong></td>
                                    <td>{{ borrow.asset.department.name|default:"未分配" }}</td>
                                </tr>
                            </table>
                        </div>
                        
                        <div class="col-md-6">
                            <h6 class="mb-3"><i class="fas fa-user me-2"></i>借用人信息</h6>
                            <table class="table table-borderless">
                                <tr>
                                    <td width="120"><strong>借用人：</strong></td>
                                    <td>{{ borrow.borrower.get_full_name|default:borrow.borrower.username }}</td>
                                </tr>
                                <tr>
                                    <td><strong>所属部门：</strong></td>
                                    <td>{{ borrow.borrower_department.name|default:"未分配" }}</td>
                                </tr>
                                <tr>
                                    <td><strong>联系电话：</strong></td>
                                    <td>{{ borrow.contact_phone|default:"未提供" }}</td>
                                </tr>
                                <tr>
                                    <td><strong>联系邮箱：</strong></td>
                                    <td>{{ borrow.contact_email|default:"未提供" }}</td>
                                </tr>
                                <tr>
                                    <td><strong>申请时间：</strong></td>
                                    <td>{{ borrow.created_at|date:"Y-m-d H:i" }}</td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 借用详情 -->
            <div class="card mb-4">
                <div class="card-header">
                    <h6 class="mb-0"><i class="fas fa-info-circle me-2"></i>借用详情</h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <td width="120"><strong>借用目的：</strong></td>
                                    <td><span class="badge bg-info">{{ borrow.get_purpose_display }}</span></td>
                                </tr>
                                <tr>
                                    <td><strong>当前状态：</strong></td>
                                    <td>
                                        {% if borrow.status == 'pending' %}
                                            <span class="badge bg-secondary">{{ borrow.get_status_display }}</span>
                                        {% elif borrow.status == 'approved' %}
                                            <span class="badge bg-success">{{ borrow.get_status_display }}</span>
                                        {% elif borrow.status == 'borrowed' %}
                                            <span class="badge bg-primary">{{ borrow.get_status_display }}</span>
                                        {% elif borrow.status == 'returned' %}
                                            <span class="badge bg-info">{{ borrow.get_status_display }}</span>
                                        {% elif borrow.status == 'overdue' %}
                                            <span class="badge bg-warning">{{ borrow.get_status_display }}</span>
                                        {% elif borrow.status == 'rejected' %}
                                            <span class="badge bg-danger">{{ borrow.get_status_display }}</span>
                                        {% else %}
                                            <span class="badge bg-light text-dark">{{ borrow.get_status_display }}</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>预计归还：</strong></td>
                                    <td>{{ borrow.expected_return_date|date:"Y-m-d H:i" }}</td>
                                </tr>
                                {% if borrow.is_overdue %}
                                <tr>
                                    <td><strong>逾期天数：</strong></td>
                                    <td><span class="text-danger"><strong>{{ borrow.days_overdue }} 天</strong></span></td>
                                </tr>
                                {% endif %}
                            </table>
                        </div>
                        
                        <div class="col-md-6">
                            {% if borrow.borrowed_at %}
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="120"><strong>借出时间：</strong></td>
                                        <td>{{ borrow.borrowed_at|date:"Y-m-d H:i" }}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>办理人：</strong></td>
                                        <td>{{ borrow.borrowed_by.get_full_name|default:borrow.borrowed_by.username }}</td>
                                    </tr>
                                    {% if borrow.actual_return_date %}
                                    <tr>
                                        <td><strong>归还时间：</strong></td>
                                        <td>{{ borrow.actual_return_date|date:"Y-m-d H:i" }}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>借用时长：</strong></td>
                                        <td>{{ borrow.borrow_duration }} 天</td>
                                    </tr>
                                    {% endif %}
                                </table>
                            {% endif %}
                        </div>
                    </div>
                    
                    <div class="mt-3">
                        <strong>详细说明：</strong>
                        <p class="mt-2">{{ borrow.purpose_description }}</p>
                    </div>
                </div>
            </div>
            
            <!-- 审批信息 -->
            {% if borrow.approver or borrow.status == 'pending' %}
            <div class="card mb-4">
                <div class="card-header">
                    <h6 class="mb-0"><i class="fas fa-user-check me-2"></i>审批信息</h6>
                </div>
                <div class="card-body">
                    {% if borrow.approver %}
                        <div class="row">
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <td width="120"><strong>审批人：</strong></td>
                                        <td>{{ borrow.approver.get_full_name|default:borrow.approver.username }}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>审批时间：</strong></td>
                                        <td>{{ borrow.approved_at|date:"Y-m-d H:i" }}</td>
                                    </tr>
                                    <tr>
                                        <td><strong>审批结果：</strong></td>
                                        <td>
                                            {% if borrow.status == 'approved' %}
                                                <span class="badge bg-success">通过</span>
                                            {% elif borrow.status == 'rejected' %}
                                                <span class="badge bg-danger">拒绝</span>
                                            {% endif %}
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                        {% if borrow.approval_notes %}
                            <div class="mt-3">
                                <strong>审批备注：</strong>
                                <p class="mt-2">{{ borrow.approval_notes }}</p>
                            </div>
                        {% endif %}
                    {% else %}
                        <div class="alert alert-warning">
                            <i class="fas fa-clock me-2"></i>该申请正在等待审批中...
                        </div>
                    {% endif %}
                </div>
            </div>
            {% endif %}
            
            <!-- 归还信息 -->
            {% if borrow.actual_return_date %}
            <div class="card mb-4">
                <div class="card-header">
                    <h6 class="mb-0"><i class="fas fa-undo me-2"></i>归还信息</h6>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <td width="120"><strong>归还时间：</strong></td>
                                    <td>{{ borrow.actual_return_date|date:"Y-m-d H:i" }}</td>
                                </tr>
                                <tr>
                                    <td><strong>办理人：</strong></td>
                                    <td>{{ borrow.returned_by.get_full_name|default:borrow.returned_by.username }}</td>
                                </tr>
                                <tr>
                                    <td><strong>实际借用：</strong></td>
                                    <td>{{ borrow.borrow_duration }} 天</td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    
                    {% if borrow.return_condition %}
                        <div class="mt-3">
                            <strong>归还时资产状态：</strong>
                            <p class="mt-2">{{ borrow.return_condition }}</p>
                        </div>
                    {% endif %}
                    
                    {% if borrow.return_notes %}
                        <div class="mt-3">
                            <strong>归还备注：</strong>
                            <p class="mt-2">{{ borrow.return_notes }}</p>
                        </div>
                    {% endif %}
                </div>
            </div>
            {% endif %}
            
            <!-- 操作按钮 -->
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between">
                        <a href="{% url 'assets:borrow_request_list' %}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left me-1"></i>返回列表
                        </a>
                        <div>
                            {% if can_cancel %}
                                <button type="button" class="btn btn-outline-danger me-2" onclick="cancelBorrow()">
                                    <i class="fas fa-times me-1"></i>取消申请
                                </button>
                            {% endif %}
                            {% if can_approve %}
                                <a href="{% url 'assets:borrow_request_approve' borrow.pk %}" class="btn btn-success">
                                    <i class="fas fa-check me-1"></i>审批
                                </a>
                            {% elif can_borrow_out %}
                                <a href="{% url 'assets:borrow_asset_out' borrow.pk %}" class="btn btn-info">
                                    <i class="fas fa-arrow-right me-1"></i>办理借出
                                </a>
                            {% elif can_return %}
                                <a href="{% url 'assets:return_asset' borrow.pk %}" class="btn btn-warning">
                                    <i class="fas fa-arrow-left me-1"></i>办理归还
                                </a>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function cancelBorrow() {
    if (confirm('确定要取消这个借用申请吗？此操作不可撤销。')) {
        // 这里可以添加取消申请的AJAX请求
        // 暂时使用简单的提示
        alert('取消功能开发中，请联系管理员处理。');
    }
}
</script>
{% endblock %}
